/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.emails;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 *
 * @author Виталий
 */
public class Email implements EmailTemplates {

    Properties properties;
    String toAddress = null;
    String[] toAddresses = null;
    String subject;
    String file;
    String message;

    /**
     * @param rn E-mail address of receiver
     * @param s E-mail subject
     * @param f file contains E-mail text
     */
    public Email(String rn, String s, String f) {
        toAddress = rn;
        subject = s;
        file = f;
        try {
            URL url = new URL(f);
            InputStream in = url.openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            StringBuffer buf = new StringBuffer();
            while (true) {
                String line = br.readLine();
                if (line != null) {
                    buf.append(line).append('\n');
                } else {
                    break;
                }
            }
            message = buf.toString();
        } catch (Exception ex) {
        }
        properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.ssl.trust", host);
    }

    /**
     * @param rn E-mail list of addresses of receivers
     * @param s E-mail subject
     * @param f file contains E-mail text
     */
    public Email(String[] rn, String s, String f) {
        toAddresses = rn;
        subject = s;
        file = f;
        properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.ssl.trust", host);
    }

    public void sendEmail() throws AddressException, MessagingException {
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };
        Session session = Session.getInstance(properties, auth);
        Message msg = new MimeMessage(session);
        msg.setSubject(subject);
        msg.setContent(message, "text/html");
        msg.setFrom(new InternetAddress(userName));
        String[] toAddressSplited = toAddress.split(" ");
        for (int c = 0, e = toAddressSplited.length; c < e; c++) {

            InternetAddress[] IntAddr = {new InternetAddress(toAddressSplited[c])};
            msg.setRecipients(Message.RecipientType.TO, IntAddr);
            Transport.send(msg);
        }
    }
}
